
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import entity.Student;

import java.io.*;
import java.util.*;

public class GSonStore {
    private final Gson _gson = new Gson();
    public void write(List<Student> students) {
        try(BufferedWriter bw = new BufferedWriter(new FileWriter("student.json"))){
            bw.write(_gson.toJson(students));
        }catch (IOException e){
            System.out.println(e.getMessage());
        }

    }

    public List<Student> read(){
        try(BufferedReader br = new BufferedReader(new FileReader("student.json"))){
            String str = br.readLine();
            return _gson.fromJson(str, new TypeToken<List<Student>>(){}.getType());
        }catch (IOException e){
            System.out.println(e.getMessage());
        }
        
        return new ArrayList<>();
    }
}
